<template>
	<view style="padding-top: 20rpx;">
		<view v-if="personalInfo" style="background-color: #FFFFFF;display: flex;align-items: center;justify-content: center;flex-direction:column;width: 90%;margin: auto;padding-top: 20rpx;border-radius: 20rpx;">
			<view style="font-size: 32rpx;font-weight: bold;color: #424C4C;margin-bottom: 19rpx;">{{personalInfo.name}}</view>
			<view style="font-size: 32rpx;font-weight: 400;color: #424C4C;">
				<text style="margin-right: 20rpx;">{{personalInfo.sex}}</text>	
				<text style="margin-right: 20rpx;">{{parseInt(personalInfo.age/12)}}岁</text>
				<text>重庆市</text>
			</view>
			<view style="margin-top: 32rpx;width: 100%;padding-left: 40rpx;box-sizing: border-box;">
				<view style="color: #BBBBBB;font-size: 26rpx;margin-bottom: 25rpx;">症状描述</view>
				<view style="width: calc(100% - 40rpx);word-wrap:break-word;font-size: 26rpx;color: #424C4C;">
					{{problem}}
				</view>
			<!-- {{problem}} -->
			</view>
			<view style="margin-top: 32rpx;width: 100%;padding-left: 40rpx;box-sizing: border-box;">
				<view style="color: #BBBBBB;font-size: 26rpx;margin-bottom: 25rpx;">当前排队人数</view>
				<view style="width: calc(100% - 40rpx);word-wrap:break-word;font-size: 26rpx;color: #424C4C;" v-if="lineUp>-1">
					{{lineUp}}人
				</view>
			</view>
			<view style="text-align: right;width: 100%;padding-right: 40rpx;margin-top: 20rpx;padding-bottom: 40rpx;color: #2A82E4;font-size: 26rpx;box-sizing: border-box;" 
				@click="tojiuzxq">查看我的就诊资料>
			</view>
		</view>
		<chat ref="chat" :username="username" :orderExt="orderExt"  chatType="singleChat"></chat> 
		<view class="win flex" v-if="getUserWin" @touchmove.stop="">
			<view style="margin: auto;background-color: #FFFFFF;border-radius: 20rpx;width: 90%;height: 400rpx;display: flex;flex-direction: column;align-items: center;justify-content: space-around;">
				<view style="font-size: 38rpx;text-align: center;font-weight: bolder;">温馨提示</view>
				<view style="font-size: 34rpx;text-align: center;">您尚未登录，请先登录...</view>
				<button class='login_btn' style="background-color: #40D596;border-radius: 50rpx;width: 300rpx;height: 100rpx;line-height: 100rpx;" 
					type='primary' open-type="getPhoneNumber"  @getphonenumber="getPhoneNumber">
					立即登录
				</button>
			</view>
		</view>
		<!-- 然后在这里传递-->
	</view>
</template>

<script>
	import chat from "@/comps/chat/chat.vue";
	let disp = require("../../utils/broadcast");
	let WebIM = require("../../utils/WebIM")["default"];
	const WxAuth =  require('@/utils/wxlogin');
	export default {
		components: {
			chat
		},
		data() {
			return {
				getUserWin: false,
				username: {
					your: "",
				},
				orderExt: {},
				personalInfo: null,
				problem: '',
				recipeId: null,
				orderId: null,
				lineUp: -1
			}
		},
		onLoad(options) {
			var that = this;
			
			if(typeof options.username!='undefined'){
				var data =  JSON.parse(options.username);
				console.log(data)
				let userId = uni.getStorageSync("usrUserId");
				if(userId){
					this.loginHX(userId)
				}else{
					WxAuth.onLogin()
					setTimeout(()=>{
						this.getUserWin = true
					},500)
				}
			
				
				
				var username = {};
				username.myName = data.userId;
				username.your = data.doctorId;
				that.username = username;
				uni.setNavigationBarTitle({
					title: data.doctorNickname
				});
				var orderExtObj = {};
				orderExtObj.orderId = data.orderId;
				orderExtObj.messageType = data.messageType;
				orderExtObj.doctorId = data.doctorId;
				orderExtObj.doctorHead = data.doctorHead;
				orderExtObj.doctorNickname = data.doctorNickname;
				orderExtObj.userId = data.userId;
				orderExtObj.userHead = data.userHead;
				orderExtObj.userNickname = data.userNickname;
				
				orderExtObj.ordStatus = data.ordStatus
				
				that.orderExt = orderExtObj;
				
				this.WxQueryPersonalId(data.orderId)
				this.getLineUpCount(data.orderId,data.doctorId)
				
				uni.setStorageSync('chatorderId',data.orderId)
			}
		},
		onUnload() {
			disp.fire("em.chatroom.leave");
		},
		methods: {
			getPhoneNumber(e){
				var that = this;
				if(e.detail.errMsg =="getPhoneNumber:ok"){
					that.encryptedData = e.detail.encryptedData;
					that.iv = e.detail.iv;
					that.getCode(e.detail.encryptedData,e.detail.iv,this.hspId);
				}
			},
			//获取用户id并存入缓存
			getCode(encryptedData,iv,hspId){
				var that = this;
				 WxAuth.checkSession().then(code => {
					 WxAuth.getUserIdByPhone(code,encryptedData,iv,hspId).then(res =>{
							if(res.success){
								that.loginHX(res.result);//登录环信
								that.getUserWin = false
								uni.showToast({
									title: '登录成功',
									icon: 'success'
								})
							}else{
								uni.showToast({
									title: '登录失败',
									icon: 'error'
								})
							}
						})
				 });
			},
			gethx(userId){
				let openid=uni.getStorageSync('openId')
				setTimeout(()=>{
					this.func.$request('login/hxLogin.tz?usrUserId=' + userId,{},'POST').then(item=>{
						var test_account = userId + '';
						uni.setStorageSync("test_psword", item.data.errMsg)
					})
				},2000)
				
			},
			loginHX(userId){
			
				var test_account = userId+'';
				let test_psword=''
				if(uni.getStorageSync("test_psword")){
					test_psword = uni.getStorageSync("test_psword")
				}else{
					this.gethx(userId)
					test_psword = uni.getStorageSync("test_psword")
				}
				uni.setStorageSync("usrUserId",test_account)
				uni.setStorageSync("myUsername",test_account);
				this.$conn.open({
					apiUrl: this.$im.config.apiURL,
					user: test_account,
					pwd: test_psword,
					grant_type: "password",
					appKey: this.$im.config.appkey
				});
			},
			tojiuzxq(){
				console.log('tojiuzxq')
				if(this.recipeId){
					uni.navigateTo({
						url: '/pages2/jiuzxqfz/index?orderId=' + this.orderId+'&usrPersonId='+this.personalInfo.id+'&isXF=1'+'&recipeId='+this.recipeId
					});
				}else{
					uni.navigateTo({
						url: '/pages2/jiuzxqfz/index?orderId=' + this.orderId+'&usrPersonId='+this.personalInfo.id+'&isXF=0'
					});
				}
			},
			WxQueryPersonalId(orderId){
				this.orderId = orderId
				this.func.$request('wx/WxQueryPersonalId.tz',{orderId : orderId},'GET').then(res=>{
					if(res.data.success){
						console.log('WxQueryPersonalId',res.data.result)
						this.personalInfo = res.data.result.per;
						this.problem = res.data.result.problem
						this.recipeId =  res.data.result.recipeId
						// that.collectForDoctor = res.data.result;
					}
				})
			},
			getLineUpCount(orderId,docId){
				this.func.$request('wx/getLineUpCount.tz',{orderId : orderId,medDocId: docId},'GET').then(res=>{
					if(res.data.success){
						// console.log('getLineUpCount',res.data.result)
						this.lineUp = res.data.result
					}
				})
			},
			onPullDownRefresh: function() {
				uni.showNavigationBarLoading();
				//这里触发chat组件的getMore方法 --xyliu
				this.$refs.chat.getMore();
				// 停止下拉动作
				uni.hideNavigationBarLoading();
				uni.stopPullDownRefresh();
			},
			
		}
	}
</script>

<style lang="less" scoped>
page{
	background-color: #FAFAFA;
}

	.win {
		position: fixed;
		width: 100%;
		height: 100vh;
		left: 0;
		top: 0;
		z-index: 20;
		background: rgba(0, 0, 0, 0.3);
	
		scroll-view {
			height: 100vh;
			background: #fff;
	
			view {
				height: 90rpx;
				line-height: 90rpx;
				border-bottom: 1rpx solid #EFEFEF;
				position: relative;
			}
	
			view.active::after {
				position: absolute;
				content: '';
				width: 65rpx;
				height: 10rpx;
				background: #74a6fd;
				left: 50%;
				margin-left: -32rpx;
				top: 65%;
			}
		}
	}

</style>
